package ssm.comfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import ssm.interceptor.CorsInterceptor;
import ssm.interceptor.LoginInterceptor;

/**
 * 注册拦截器，让自定义的拦截生效
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    CorsInterceptor corsInterceptor() {
        return new CorsInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 拦截全部路径，这个解决跨域
        registry.addInterceptor(corsInterceptor())
                        .addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor())
                // 添加要拦截的路径，即url
                .addPathPatterns("/api/v1/pri/*/*/**")
                // 添加要放行的路径，即有些pri的url不需要拦截
                .excludePathPatterns("/api/v1/pri/user/register", "/api/v1/pri/user/login");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
